var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    adressInfoDataList:[
      // {
      //   id:1,
      //   checked: true,
      //   name: "张三",
      //   gender:"先生",
      //   phone:"18634562235",
      //   adressName:"德容科技大厦",
      //   houseNumber:"2楼三单元5034",
      //   detailedAddress:"",
      //   label:"家"
      // }, {
      //   id: 2,
      //   checked: false,
      //   name: "李四",
      //   gender: "女士",
      //   phone: "18634564444",
      //   adressName: "北京师范大学",
      //   houseNumber: "100楼34单元6664",
      //   detailedAddress: "",
      //   label: "学校"
      // }
    ],
    adressInfoDatais:null,
  },
  radioChange: function (e) {
//console.log(e.detail.value);
    let ind = e.detail.value;
    let that = this;
    for (let i = 0; i < that.data.adressInfoDataList.length;i++){
      that.data.adressInfoDataList[i].is_default = '0';
    }
    that.data.adressInfoDataList[ind].is_default = '1';
// console.log('被选中的地址：', that.data.adressInfoDataList[ind]); 
    that.setData({
      adressInfoDataList: that.data.adressInfoDataList
    });

    let adressInfoData = wx.getStorageSync('adressInfoData');
    that.data.adressInfoDatais = adressInfoData[ind];

    that.setData({
      adressInfoDatais: that.data.adressInfoDatais
    });
    this.address_edit_is(1);
    let pages = getCurrentPages();//当前页面栈
    let prevPage = pages[pages.length - 2];//上一页面
    prevPage.onLoad();
  },
  address_edit_is(num) {
    let that = this;
    let k = wx.getStorageSync('key');
    // console.log('被选中的地址：', that.data.adressInfoDataList[ind]);
    var addr = that.data.adressInfoDatais 
    wx.request({
      url: ApiUrl + '/index.php?act=member_address&op=address_edit',
      data: {
        key: k,
        address_id: addr.address_id,//地址编号
        true_name: addr.true_name,//姓名
        city_id: addr.city_id, //城市编号(地址联动第二级)
        area_id: addr.area_id, //地区编号(地址联动最后一级)
        area_info: addr.area_info, //地区信息，例：天津 天津市 红桥区
        address: addr.address, //地址信息，例：水游城8层
        tel_phone: addr.tel_phone, //电话号码
        mob_phone: addr.mob_phone,//手机
        is_default: num, //是否为默认收货地址 1是 0否
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",//get为默认方法/POST
      success: function (result) {
        if (result.data.datas == 1) {
          // wx.navigateTo({
          //   url: '/pages/adressInfo/adressInfo'
          // })
          that.addAdressInfo();
        }
//console.log('地址默认：', result.data.datas);
      }
    });
  },
  gaixie_adressbtn(e){
//console.log(e.currentTarget.dataset.id);
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../pages/add_address/add_address?id=' + id,
    })
  },
  add_adressbtn(){
    wx.navigateTo({
      url: '../../pages/add_address/add_address'
    })
  },
  addAdressInfo(){
    wx.showLoading({
      title: "加载中..."
    })
    let that = this;
    let k = wx.getStorageSync('key'); 
    wx.request({//index.php?act=member_address&op=address_list
      url: ApiUrl + '/index.php?act=member_address&op=address_list',
      data: { key: k  },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
//console.log('地址列表==：',result);
        if (result.statusCode === 200){
          if (result.data.code === 200){
            let data = result.data.datas.address_list;
            wx.hideLoading();
            that.data.adressInfoDataList = data;
            wx.setStorageSync('adressInfoData', that.data.adressInfoDataList);
          }
        }
        that.setData({
          adressInfoDataList: that.data.adressInfoDataList
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.addAdressInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.addAdressInfo();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})